//
//  SinaNewFeatureCollectionViewController.swift
//  weibo
//
//  Created by liuchao on 16/1/24.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
let count = 4
class SinaNewFeatureCollectionViewController: UICollectionViewController {

    let flowLayout=UICollectionViewFlowLayout()
    // Initializer does not override a designated initializer from its superclass
    
    init(){
    
        super.init(collectionViewLayout:flowLayout)
        
    }


    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    
    }

    private func setupUI(){
     
        // Register cell classes
        self.collectionView!.registerClass(SinaNewFeatureCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
      // 设置分页，设置边界，隐藏滚动条，
        
        collectionView?.pagingEnabled=true
        collectionView?.bounces=false
        
        collectionView?.showsHorizontalScrollIndicator=false
        collectionView?.showsVerticalScrollIndicator=false
        
        
        // 在布局的基础上设置item大小及 滚动方向 和多个item之间的间距
         flowLayout.itemSize = UIScreen.mainScreen().bounds.size
        
        flowLayout.scrollDirection=UICollectionViewScrollDirection.Horizontal
        flowLayout.minimumInteritemSpacing=0
        flowLayout.minimumLineSpacing=0
        
    }
   
}

// 扩张类方法
extension SinaNewFeatureCollectionViewController{

    // MARK: UICollectionViewDataSource
    
//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//
//        return 0
//    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SinaNewFeatureCollectionViewCell
        
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.yellowColor()
        
        cell.index=indexPath.item

        return cell
    }
}

class SinaNewFeatureCollectionViewCell:UICollectionViewCell{
  
    var index:Int=0 {
        
        didSet{
            
            iconImageView.image=UIImage(named: "new_feature_\(index+1)")
            if index==3{
               enterButton.hidden=false
            }else{
               enterButton.hidden=true
            }
        }
    }
    
    // 设置 初始化
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
      
         addSubview(iconImageView)
        addSubview(enterButton)
        
        // 设置约束 -- 直接设置 frame
        iconImageView.frame = bounds
        //设置 约束 -- 水平居中 ,距离cell的底部 150
        
        //第一步一定要关掉 translatesAutoresizingMaskIntoConstraints
        enterButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 这个约束是添加到 button上还是 添加到 button的父试图 cell上
        // 约束要添加到 cell,!!!!!!!!!!!千万不要添加到 enterButton.addConstraint
        addConstraint(NSLayoutConstraint(item: enterButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: enterButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -150))
    }
    
    // 懒加载 图片
    lazy var iconImageView:UIImageView={
    
          let img = UIImageView()
        
          return img
    }()
    //  懒加载 按钮，并设置按钮的背景图片和背景文字 以及点击效果
    lazy var enterButton: UIButton = {
    
        let button=UIButton(type: UIButtonType.Custom)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Normal)
    
        button.setTitle("dianjijingru", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        
        button.sizeToFit()
        button.addTarget(self, action: "enter", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    func enter(){
        
        printLog("点击进入!!!!")
        NSNotificationCenter.defaultCenter().postNotificationName(kNotificationEnter, object: nil)
    }

}
